package com.whut.monitor.service.impl;

import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.dao.IBaseDao;
import com.whut.monitor.model.CompensatorEntity;
import com.whut.monitor.service.ICompensatorService;
import com.whut.monitor.vo.CompensatorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author CFlower on 2017/4/5
 * 补偿器服务层实现
 */

@Service
public class CompensatorServiceImpl implements ICompensatorService{

    @Autowired
    private IBaseDao<CompensatorEntity> compensatorDao;

    @Override
    public void add(CompensatorVo compensatorVo) {
        CompensatorEntity entity = new CompensatorEntity();
        entity.setCompensatorNum(compensatorVo.getCompensatorNum());
        entity.setCompensatorName(compensatorVo.getCompensatorName());
        entity.setCompany(compensatorVo.getCompany());
        entity.setProductDate(compensatorVo.getProductDate());
        compensatorDao.save(entity);
    }

    @Override
    public void delete(Integer id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        compensatorDao.executeHql("delete from CompensatorEntity t where t.id=:id",params);
    }

    @Override
    public void edit(CompensatorVo compensatorVo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("compensatorName", compensatorVo.getCompensatorName());
        params.put("compensatorNum", compensatorVo.getCompensatorNum());
        params.put("productDate", compensatorVo.getProductDate());
        params.put("company", compensatorVo.getCompany());
        params.put("id", compensatorVo.getId());
        String hql="update CompensatorEntity e set e.compensatorName=:compensatorName,e.compensatorNum=:compensatorNum"
                + ",e.productDate=:productDate,e.company=:company where e.id=:id";
        compensatorDao.executeHql(hql, params);
    }

    @Override
    public List<CompensatorVo> dataGrid(CompensatorVo compensatorVo, PageFilter pageFilter){
        List<CompensatorVo> ul = new ArrayList<CompensatorVo>();
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from CompensatorEntity t ";
        List<CompensatorEntity> list = compensatorDao.findList(hql + whereHql(compensatorVo, params) + orderHql(pageFilter), params, pageFilter.getPage(), pageFilter.getRows());
        for (CompensatorEntity t : list) {
            CompensatorVo u = new CompensatorVo();
            BeanUtils.copyProperties(t, u);

            ul.add(u);
        }
        return ul;
    }

    @Override
    public Long count(CompensatorVo compenstr, PageFilter ph) {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = " from CompensatorEntity t ";
        return compensatorDao.count("select count(*) " + hql + whereHql(compenstr, params), params);
    }

    private String whereHql(CompensatorVo compensator, Map<String, Object> params) {
        String hql = "";
        if (compensator != null) {
            hql += " where 1=1 ";
            if (compensator.getCompensatorName() != null) {
                hql += " and t.name like :name";
                params.put("name", "%%" + compensator.getCompensatorName() + "%%");
            }
        }
        return hql;
    }

    private String orderHql(PageFilter ph) {
        String orderString = "";
        if ((ph.getSort() != null) && (ph.getOrder() != null)) {
            orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
        }
        return orderString;
    }

    @Override
    public List<Tree> tree() {
        List<CompensatorEntity> l = null;
        List<Tree> lt = new ArrayList<Tree>();

        l = compensatorDao.findList("select distinct entity from CompensatorEntity entity");

        if ((l != null) && (l.size() > 0)) {
            for (CompensatorEntity r : l) {
                Tree tree = new Tree();
                tree.setId(r.getId().toString());
                tree.setText(r.getCompensatorName());
                lt.add(tree);
            }
        }
        return lt;
    }


}
